﻿using System.IO;
using System.Text.RegularExpressions;
using Raven;
using UnityEditor;
using UnityEngine;

namespace Editor
{
    public static class MapTool
    {
        [MenuItem("Tools/MapText2Byte")]
        public static void MapText2Byte()
        {
            string exportPath = Path.Combine(Application.dataPath, "ResourcesAssets/Raven_DM1.bytes");
            Stream stream = new FileStream(exportPath, FileMode.Create);
            BinaryWriter binaryWriter = new BinaryWriter(stream);

            string filePath = Path.Combine(Application.dataPath, "ResourcesAssets/Raven_DM1.map");
            StreamReader streamReader = new StreamReader(filePath);
            int numNodes = int.Parse(streamReader.ReadLine());
            binaryWriter.Write(numNodes);

            for (int n = 0; n < numNodes; ++n)
            {
                string[] buffer = streamReader.ReadLine().Split(' ');
                int index = int.Parse(buffer[1]);
                Fix x = (FixConst) float.Parse(buffer[3]);
                Fix y = (FixConst) float.Parse(buffer[5]);

                binaryWriter.Write(index);
                binaryWriter.Write(x.Raw);
                binaryWriter.Write(y.Raw);
            }

            int numEdges = int.Parse(streamReader.ReadLine());
            binaryWriter.Write(numEdges);
            for (int e = 0; e < numEdges; ++e)
            {
                string[] buffer = streamReader.ReadLine().Split(' ');
                int from = int.Parse(buffer[1]);
                int to = int.Parse(buffer[3]);
                Fix cost = (FixConst) float.Parse(buffer[5]);

                binaryWriter.Write(from);
                binaryWriter.Write(to);
                binaryWriter.Write(cost.Raw);
            }

            string[] size = streamReader.ReadLine().Split(' ');
            int sizeX = int.Parse(size[0]);
            int sizeY = int.Parse(size[1]);

            binaryWriter.Write(sizeX);
            binaryWriter.Write(sizeY);

            while (!streamReader.EndOfStream)
            {
                // todo LoadMap
                string[] buffer = Regex.Split(streamReader.ReadLine(), "\\s+", RegexOptions.IgnoreCase);
                EntityType type = (EntityType) int.Parse(buffer[0]);
                binaryWriter.Write(int.Parse(buffer[0]));
                switch (type)
                {
                    case EntityType.Wall:
                        Raven.Vector2 a = new Raven.Vector2((FixConst) float.Parse(buffer[1]),
                            (FixConst) float.Parse(buffer[2]));
                        Raven.Vector2 b = new Raven.Vector2((FixConst) float.Parse(buffer[3]),
                            (FixConst) float.Parse(buffer[4]));
                        Raven.Vector2 n = new Raven.Vector2((FixConst) float.Parse(buffer[5]),
                            (FixConst) float.Parse(buffer[6]));

                        binaryWriter.Write(a.x.Raw);
                        binaryWriter.Write(a.y.Raw);

                        binaryWriter.Write(b.x.Raw);
                        binaryWriter.Write(b.y.Raw);

                        binaryWriter.Write(n.x.Raw);
                        binaryWriter.Write(n.y.Raw);
                        break;
                    case EntityType.SpawnPoint:
                        Raven.Vector2 p = new Raven.Vector2((FixConst) float.Parse(buffer[2]),
                            (FixConst) float.Parse(buffer[3]));
                        binaryWriter.Write(p.x.Raw);
                        binaryWriter.Write(p.y.Raw);
                        break;
                    case EntityType.Health:
                    {
                        Fix x = (FixConst) float.Parse(buffer[2]);
                        Fix y = (FixConst) float.Parse(buffer[3]);
                        Fix r = (FixConst) float.Parse(buffer[4]);
                        Fix h = (FixConst) float.Parse(buffer[5]);
                        int GraphNodeIndex = int.Parse(buffer[6]);
                        binaryWriter.Write(x.Raw);
                        binaryWriter.Write(y.Raw);
                        binaryWriter.Write(r.Raw);
                        binaryWriter.Write(h.Raw);
                        binaryWriter.Write(GraphNodeIndex);
                    }
                        break;
                    case EntityType.RocketLauncherWeaponGiver:
                    {
                        Fix x = (FixConst) float.Parse(buffer[2]);
                        Fix y = (FixConst) float.Parse(buffer[3]);
                        Fix r = (FixConst) float.Parse(buffer[4]);
                        int GraphNodeIndex = int.Parse(buffer[5]);
                        binaryWriter.Write(x.Raw);
                        binaryWriter.Write(y.Raw);
                        binaryWriter.Write(r.Raw);
                        binaryWriter.Write(GraphNodeIndex);
                    }
                        break;

                    case EntityType.ShotgunWeaponGiver:
                    {
                        Fix x = (FixConst) float.Parse(buffer[2]);
                        Fix y = (FixConst) float.Parse(buffer[3]);
                        Fix r = (FixConst) float.Parse(buffer[4]);
                        int GraphNodeIndex = int.Parse(buffer[5]);
                        binaryWriter.Write(x.Raw);
                        binaryWriter.Write(y.Raw);
                        binaryWriter.Write(r.Raw);
                        binaryWriter.Write(GraphNodeIndex);
                    }
                        break;

                    case EntityType.RailGunWeaponGiver:
                    {
                        Fix x = (FixConst) float.Parse(buffer[2]);
                        Fix y = (FixConst) float.Parse(buffer[3]);
                        Fix r = (FixConst) float.Parse(buffer[4]);
                        int GraphNodeIndex = int.Parse(buffer[5]);
                        binaryWriter.Write(x.Raw);
                        binaryWriter.Write(y.Raw);
                        binaryWriter.Write(r.Raw);
                        binaryWriter.Write(GraphNodeIndex);
                    }
                        break;
                }
            }

            streamReader.Close();
            binaryWriter.Close();
            stream.Close();
            EditorUtility.DisplayDialog("tips", "success", "ok");
        }
    }
}